/********************************************************************************
 * Create by   : Andy So
 * Date        : 01-Nov-2004
 *
 * Copyright (c) 2004 by PCCW Limited
 *
 * All rights reserved. All information contained in this software is confidential and proprietary to
 * PCCW Limited. No part of this software may be reproduced or transmitted in any form or any means,
 * electronic, mechanical, photocopying, recording or otherwise stored in any retrieval system of any
 * nature without the prior written permission of PCCW Limited.
 *
 * This material is a trade secret and its confidentiality is strictly maintained. Use of any copyright
 * notice does not imply unrestricted public access to this material.
 ********************************************************************************
 */

package com.songqintuan.common.util;


import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Locale;

public class FormatUtil {

	public static String getFormatDate(Date date, String pattern) {
		String fd = null;
		if (date != null) {
			SimpleDateFormat sd = new SimpleDateFormat(pattern, Locale.ENGLISH);
			fd = sd.format(date);
		}
		return fd;
	}

 /*
   *
   * format a numeric string
   *
   * <p>
   * @param     pattern       "00", "-00.00", '$###,###,##0.00", etc
   * ///////////////////////////////////////////
   *       Symbol Meaning
   *       0      a digit
   *       #      a digit, zero shows as absent
   *       .      placeholder for decimal separator
   *       ,      placeholder for grouping separator.
   *       E      separates mantissa and exponent for exponential formats.
   *       ;      separates formats.
   *       -      default negative prefix.
   *       %      multiply by 100 and show as percentage
   *       ?      multiply by 1000 and show as per mille
   *       ?     currency sign; replaced by currency symbol; if
   *              doubled, replaced by international currency symbol.
   *              If present in a pattern, the monetary decimal separator
   *              is used instead of the decimal separator.
   *       X      any other characters can be used in the prefix or suffix
   *       '      used to quote special characters in a prefix or suffix.
   * ///////////////////////////////////////////
   * @return 	  a formatted String w.r.t. the pattern being defined
   * @exception ParseException
   * @author 	  Vincent Yuen
   * @version 	%I%, %G%
   * @see       java.text.DecimalFormat
   */
  public static String formatNumber(String pattern, double num) {
      /** Re-implemented by Forrest Chen GZ **/
      String outStr = "";
      String finalPattern = pattern;
      if(pattern!=null)
      {
          int idx = pattern.indexOf(".");
          if(idx!=-1)
          {              
              String pre = "";
              if(idx==0)
                  pre = "0";
              else
                  pre = pattern.substring(0,idx-1);
              
              String suf = pattern.substring(idx);
              finalPattern = pre+"0"+suf;
          }
          DecimalFormat formatter = new DecimalFormat(finalPattern);
          outStr = formatter.format(num);
      }
      
      return outStr;
    /* Commented all by Forrest Chen GZ   
    DecimalFormat formatter = new DecimalFormat(pattern);
    String outStr="";
    try {
      outStr = formatter.format(num);
      if(num<1){
          outStr = "0" + outStr;
      }
    } catch (Exception pe) {
    //  Debug.print(pe);
    }
      return outStr;
    */
  }

    public static double formatDouble(String pattern, double num) {
        DecimalFormat formatter = new DecimalFormat(pattern);
        String outStr="";
        try {
            outStr = formatter.format(num);
        } catch (Exception pe) {
            //  Debug.print(pe);
        }
        return Double.parseDouble(outStr);
    }
    
    public static void main(String[] args)
    {
        System.out.println(FormatUtil.formatNumber(".00",12220.32));
        System.out.println(FormatUtil.formatNumber("#.00",-0.32));
    }
}
